package com.jt.controller;

import com.jt.pojo.SearchResult;
import com.jt.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * http://www.jt.com/products/1.html
 * @author dc
 * @Classname ItemCatController
 * @Date 2020/5/6 10:22
 */
@Controller
@RequestMapping("/products")
public class ItemCatController {

    @Autowired
    ItemCatService itemCatService;

    @RequestMapping("/{itemCatId}")
    public String getItemListByCatId(
            @PathVariable Long itemCatId,
            @RequestParam(defaultValue = "1",required =false) Integer page,
            @RequestParam(defaultValue = "20",required = false) Integer rows,
            Model model){
        SearchResult itemListByItemCatId = itemCatService.getItemListByItemCatId(itemCatId,page,rows);
        String name=itemCatService.getItemNameByCatId(itemCatId);
        //回显查询条件
        model.addAttribute("query",name);
        model.addAttribute("itemList",itemListByItemCatId.getItemList());
        model.addAttribute("page",page);
        model.addAttribute("totalPages",itemListByItemCatId.getPageCount());
        model.addAttribute("rows",rows);
        model.addAttribute("isItemCat",itemCatId);
        model.addAttribute("recordCount",itemListByItemCatId.getRecordCount());
        if(page<itemListByItemCatId.getPageCount()){
            model.addAttribute("nextPage",page+1);
        }
        if(page>1){
            model.addAttribute("lastPage",page-1);
        }
        return "search";
    }
}
